<?php 
session_start();
?><pre><?php
$file = file_get_contents('tags.csv');
$lines = explode("\r", $file);

$columns = array_shift($lines);

$records = array();
foreach ($lines as $line) {
    $records[] = explode(";", $line);
}

$shows = array();
$showsMatch = array();
$tags = array();

foreach ($records as $record) {
    $show = array_shift($record);
    
    if($show)
    {
    
    
        foreach ($record as $tag) {
            $tag = trim(strtolower($tag));
            if($tag){
                $shows[$show][] = $tag;
                if(!isset($tags[$tag])){
                    $tags[$tag] = 0;
                }
                $tags[$tag]++;
            }
        }
        
        sort($shows[$show]);
    }
}

//echo "\n\ntags & count: \n";
//arsort($tags);
//print_r($tags);
//
//echo "\n\nshows & tags: \n";
//print_r($shows);


foreach ($shows as $show=>$showTags) {
    $showsMatch[$show] = array();
    foreach ($shows as $mshow=>$mshowTags) {
        if($mshow != $show){
            $showsMatch[$show][$mshow] = 0;
            foreach ($showTags as $stag) {
                foreach ($mshowTags as $mstag) {
                    if($mstag == $stag){
                        $showsMatch[$show][$mshow]++;
                    }
                }
            }
            arsort($showsMatch[$show]);
        }
        
    }   
}


//echo "\n\nshows macthing: \n";
//print_r($showsMatch);

//$spilttags = array('dag','nacht','avond','ochtend');
$spilttags = array('dag','nacht','avond','ochtend');
$splitres = array();

foreach ($shows as $show=>$stags) {
    $matched = false;
    foreach ($spilttags as $splittag) {
        if(in_array($splittag, $stags)){
           $splitres[$splittag][] = $show;
           $matched = true; 
        }        
    }
    if(!$matched)
    {
        $splitres['unmatched'][] = $show;
    }
}

//echo "\n\ntags split: \n";
//print_r($splitres);

$vragen = array();
$vragen[] = array("dag"=>array("ochtend","dag"), 
				  "nacht"=>array("avond","nacht"));
$vragen[] = array(   "foto_buiten_bos"=>array("natuur","actief",'sporty spice', 'dag'), 
					'foto_vriendinnen_winkelen'=>array('feest','drankje','avond','seks','massa', 'exclusief','partylover'),
                    'foto_op_de_bank_hangen'=>array('dag','bundel','eten','drankje', 'massa', 'gezellig', 'couchsurfer', 'gewoon'),
                    'foto_candlelight_dinner'=>array('romantic','romantiek','avond','intiem', 'eten', 'drankje'),
                    'foto_voeten_onder_laken'=>array('seks','spanning','experiment','drankje','exclusief'),
                    'foto_tattoo'=>array('hipster','spanning','experiment','innovatief','exclusief','drankje','performances'),
);
$vragen[] = array(   
					"verrassing_ja"=>array("experiment","verrassing","spanning","innovatief", "performances"), 
					"verrassing_nee"=>array("humor","massa","gewoon","couchsurfer","preview"), 
);
$vragen[] = array(   
					"statisch"=>array("film","fotografie","design","architectuur","schilderijen"), 
					"dynamisch"=>array("dans", "muziek", "theater", "cabaret", "toneel", "mode" ), 
);

if(isset($_GET['reset'])){
    $_SESSION['antwoorden'] = array();
    $_GET = array();
    $currentvraag = 0;
}

if(isset($_GET) && sizeof($_GET) > 0){
    
    $_SESSION['antwoorden'][$_GET['num']] = $_GET['antwoord'];
    $currentvraag = $_GET['num']+1;
}
else{
    $currentvraag = 0;
}
echo "<a name='quiz'>";
if(isset($vragen[$currentvraag])){
    foreach ($vragen[$currentvraag] as $antwoord=>$keuzes) {
        echo "\n<br /><a href='?antwoord=$antwoord&num=$currentvraag#quiz'>$antwoord</a>";
    }
}
echo "\n<br />\n<br /><a href='javascript:history.back(-1);'>< terug</a>";

$taglist = array();
if(isset($_SESSION['antwoorden'])){
    foreach ($_SESSION['antwoorden'] as $num=>$key) {
        foreach ($vragen[$num][$key] as $tag) {
            $taglist[] = $tag;
        }    
    }
}

sort($taglist);
echo "\n\ntags: \n";
print_r($taglist);

$matches = array();
foreach ($shows as $show=>$stags) {
    $matches[$show] = 0;
    foreach ($taglist as $tag) {
        if(in_array($tag, $stags)){
            $matches[$show]++;
        }
    }
}

arsort($matches);
echo "\n\nmathes: \n";
print_r($matches);
echo "\n\n";
echo "\n<br />\n<br /><a href='?reset=1#quiz'>reset</a>";


?>